package com.easysoft.framework.action;
import com.easysoft.commons.utils.StringUtils;
import com.easysoft.commons.utils.WebConstants;
import com.easysoft.framework.annotation.logs.SystemControllerLog;
import com.easysoft.framework.article.model.*;
import com.easysoft.framework.article.service.ArticleCategoryService;
import com.easysoft.framework.article.service.SysSingleService;
import com.easysoft.framework.beans.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by try on 2017/7/31.
 */
@RestController
@RequestMapping(value = SystemSingleAction.BASE_VIEW)
public class SystemSingleAction {
    public static final String BASE_VIEW = "/article";
    private final static Logger logger = LoggerFactory.getLogger(SystemSingleAction.class);

    @Autowired
    @Qualifier("service.SysSingleService")
    private SysSingleService sysSingleService;

    @Autowired
    @Qualifier("service.ArticleCategoryService")
    private ArticleCategoryService articleCategoryService;

    /**
     * 获取单页列表
     *
     * @param page
     * @param query
     * @return
     */
    @RequestMapping(value = "/listSingle", method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView listSingle(Page page, SysSingleQuery query) {
        logger.info("Call SysSingleAction.listSingle({},{})", page.toString(), query.toString());
        List<SysSingleView> listSingle = sysSingleService.selectSysSingleListByPage(page, query);
        ModelAndView mv = new ModelAndView(BASE_VIEW + "/listSingle");
        mv.addObject("page", page); // 返回分页
        mv.addObject("query", query);// 查询条件
        mv.addObject("listSingle", listSingle); // 返回查询结果集
        return mv;
    }

    @RequestMapping(value = "/editSingle", method = RequestMethod.GET)
    public ModelAndView editSingle(String action, @RequestParam(required = false) String id) {
        logger.info("Call SysSingleAction.editSingle({},{})", action, id);
        ModelAndView mv = new ModelAndView(BASE_VIEW + "/editSingle");
        List<SysArticleCategoryView> categories = articleCategoryService.selectAllCategory();
        mv.addObject("categories",categories);
        if (StringUtils.isNotNullAndBlank(action) && action.toLowerCase().equals("add")) {
            // 添加
            if (StringUtils.isNotNullAndBlank(id)) {
                return null;
            }
            return mv;
        }
        if (StringUtils.isNotNullAndBlank(action) && action.toLowerCase().equals("update")) {
            // 修改
            if (StringUtils.isNullOrBlank(id)) {
                return null;
            }
            SysSingle entity = sysSingleService.selectByPrimaryKey(id);
            if (entity == null) {
                return null;
            }
            mv.addObject("entity", entity);
            return mv;
        }
        return null;
    }

    @RequestMapping(value = "/saveSingle",method = RequestMethod.POST)
    public int saveSingle(String action,SysSingle single, HttpSession session){
        logger.info("Call SysSingleAction.saveSingle({},{})", action, single.toString());
        if (StringUtils.isNotNullAndBlank(action) && action.toLowerCase().equals("add")) {
            // 添加
            if (StringUtils.isNotNullAndBlank(single.getId())) {
                return 0;
            }
            return addSysSingle(single, session);
        }
        if (StringUtils.isNotNullAndBlank(action) && action.toLowerCase().equals("update")) {
            // 修改
            if (StringUtils.isNullOrBlank(single.getId())) {
                return 0;
            }
            return updateSingle(single, session);
        }
        return 0;
    }


    /**
     * 添加单页信息
     *
     * @param sysSingle 单页信息
     */
    @SystemControllerLog(description = "添加单页信息")
    private int addSysSingle(SysSingle sysSingle, HttpSession session) {
        logger.info("Call SysSingleAction.addSysSingle()");

        sysSingle.setOperator((String) session.getAttribute(WebConstants.SESSION_USER_ID));

        return sysSingleService.addByPrimaryKeyWithBLOBs(sysSingle);
    }

    /**
     * 获取单个单页
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/viewSingleById", method = RequestMethod.POST)
    public SysSingle viewSingleById(String id) {
        logger.info("Call SysSingleAction.viewSingleById({})" + id);
        return sysSingleService.selectByPrimaryKey(id);
    }

    /**
     * 更新单页信息
     *
     * @param sysSingle 单页信息
     */
    @RequestMapping(value = "/updateSingle",method = RequestMethod.POST)
    @SystemControllerLog(description = "更新单页信息")
    public int updateSingle(SysSingle sysSingle, HttpSession session) {
        logger.info("Call SysSingleAction.updaterSingleStatus()");
        sysSingle.setOperator((String) session.getAttribute(WebConstants.SESSION_USER_ID));
        return sysSingleService.updateBySelective(sysSingle);

    }

    /**
     * 删除单页
     *
     * @param id 单页编号
     */
    @RequestMapping(value = "/deleteSingle", method = RequestMethod.POST)
    @SystemControllerLog(description = "删除单页信息")
    public int deleteSysSingle(String id) {
        logger.info("Call SysSingleAction.deleteSysSingle({})", id);
        return sysSingleService.deleteByPrimaryKey(id);
    }
}
